<!--地址管理列表-->
<template>
  <div class="contain">
    <app-header></app-header>
    <div class="addr-row mb30" v-for="item in addlist">
      <div class="header cf ft17" @click.stop="setDefault(item)">
        <span class='f-l'>{{item.Contacts || '--'}}</span>
        <em class='f-r ft17'>{{item.ContactsTel}}</em>
      </div>
      <p class="addr ft17">{{item.Province | province}}{{item.City | city}}{{item.District | district}}  {{item.Address}}</p>
      <div class="addr-foot cf">
        <label>
          <input type="radio" class="list-radio-check" name="address" v-model="defaultAddr" :value="item" @click.stop="setDefault(item)">
          <span class="list-radio-circle"></span>
          <em class="txt">默认地址</em>
        </label>
        <div class="f-r">
          <span class="mr20" @click.stop="editAddr(item.Guid)"><i class="iconfont icon-order-entry"></i>编辑</span>
          <span @click.stop="delAddr(item.Guid)"><i class="iconfont icon-delete2"></i>删除</span>
        </div>
      </div>
    </div>
    <div class="btn-addr" @click="toAddForm">
      <i class="iconfont icon-add mr10"></i>添加新地址</div>
  </div>
</template>

<script>
import appHeader from '@/components/core/header/app_header.vue';
export default {
  components:{
    appHeader
  },
  name:'address',
  data() {
    return {
      addlist: [],
      defaultAddr:{},
    }
  },
  created() {
    this.getAddress();
  },
  computed:{
    userinfo(){
      return localStorage.getItem('userinfo') === null?null:JSON.parse(localStorage.getItem('userinfo'));
    }
  },
  methods: {
    //新增
    toAddForm() {
      this.$router.push({
        path: '/order/addressForm',
        query:{
          ccguid: this.$route.query.id,
        }
      })
    },
    //获取列表
    getAddress() {
      var self = this;
      var id = this.$route.query.id;
      var userinfo = JSON.parse(localStorage.userinfo);
      var query = {
        CCGUID: id,
      };
      this.Ajax.post('SearchCustomerAddress', query).then((resp) => {
        if (resp.IsSuccess == true) {
          this.addlist = resp.Datas;
          for (var i = 0; i < this.addlist.length;i++) {
            if(this.addlist[i].IsChecked == 'Y'){
              this.defaultAddr = this.addlist[i];
            }
          };
        }
      });
    },
    //选择地址
    setDefault(item){
      let self = this,
          query = {
            Guid: item.Guid,
            CcGuid: item.CcGuid,
            Province: item.Province,
            City: item.City,
            District: item.District,
            Address: item.Address,
            Contacts: item.Contacts,
            ContactsTel: item.ContactsTel,
            UpdateUserId: this.userinfo.StaffID,
            UpdateUserName: this.userinfo.Name,
            IsChecked: 'Y',
            ClearChecked: 'Y'
          };
      this.Ajax.post('UpdateCustomerAddress', query).then((resp) => {
        if (resp.IsSuccess) {
          this.$router.go(-1);
        }else{
          this.Toast({
            message: resp.ErrorMessage||'操作失败，请稍后重试'
          })
        }
      });
    },
    //删除地址
    delAddr(id){
      var self = this;
      var query = {
          GUID: id,
      };
      self.MessageBox.confirm('是否删除?').then(action => {
        this.Ajax.post('DeleteCustomerAddress', query).then(function(resp) {
            if (resp.IsSuccess == true) {
              self.getAddress();
            }
          });
      });
    },
    //编辑地址
    editAddr(id){
      this.$router.push({
        path: '/order/addressForm',
        query:{
          Guid:id
        }
      })
    },
  }
}
</script>